﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using MarchEstate.Manager.BusinesEntity;
using CoreFrame.Core.Orm;
using MarchEstate.Manager.Dao.Cash;
using CoreFrame.Core.Aop;
using CoreFrame.Core.Transaction;

namespace MarchEstate.Manager.ObjectModel.Cash
{
    public class ChargeMoney : AbstractChargeMoney
    {
        private CashManagerDao cashDao = null;

        public ChargeMoney()
        {
        }

        public override void InitDao()
        {
            base.InitDao();
            cashDao = NewDao<CashManagerDao>();
        }

        [AOP(typeof(AopTransaction))]
        public override bool ProcessChargeMoney(AbstractChargeMoney charge, List<string> headIds)
        {
            //1.保存结算头
            charge.save();
            //2.保存结算明细
            for (int i = 0; i < charge.ChargeMoneyDetailList.Count; i++)
            {
                charge.ChargeMoneyDetailList[i].ChargeID = charge.ChargeID;
                charge.ChargeMoneyDetailList[i].save();
            }
            AbstractItemFeeHead head = NewObject<AbstractItemFeeHead>();
            //3.反写费用头表chargeId
            for (int i = 0; i < headIds.Count; i++)
            {
                head.UpdateChargeId(headIds[i], charge.ChargeID);
            }
            //4.修改WY_OSRCCItemRelation表的值
            AbstractOSRCCItemRelation osrcc = NewObject<AbstractOSRCCItemRelation>();
            for (int i = 0; i < charge.ChargeMoneyDetailList.Count; i++)
            {
                AbstractOSRCCItemRelation _osrcc = osrcc.GetSrccItem(charge.ChargeMoneyDetailList[i].SrccId, charge.ChargeMoneyDetailList[i].ItemCode);
                if (_osrcc != null)
                {
                    _osrcc.Lastmaturity = DateTime.Now;
                    _osrcc.Thismaturity = DateTime.Now;
                    _osrcc.Lastsurplus = charge.ChargeMoneyDetailList[i].PrepayFee;
                    _osrcc.LastLateFee = 0;
                    _osrcc.ThisCostFee = 0;
                    _osrcc.ThisLateFee = 0;
                    _osrcc.FavourFee = 0;

                    //实收金额(暂)=应收金额+滞纳金-上次余额-上次滞纳金-优惠金额
                    //（如果 实收金额(暂)>=0）  实收金额=实收金额(暂) 、 本次余额=0
                    //（如果 实收金额(暂)<0 ）  实收金额=0 、 本次余额=实收金额+优惠金额+上次余额+上次滞纳金-应收金额-滞纳金
                    decimal _RealityFee = _osrcc.ThisCostFee + _osrcc.ThisLateFee - _osrcc.Lastsurplus - _osrcc.LastLateFee - _osrcc.FavourFee;
                    if (_RealityFee >= 0)
                    {
                        _osrcc.RealityFee = _RealityFee;
                        _osrcc.PrepayFee = 0;
                    }
                    else
                    {
                        _osrcc.RealityFee = 0;
                        _osrcc.PrepayFee = _osrcc.RealityFee + _osrcc.FavourFee + _osrcc.Lastsurplus + _osrcc.LastLateFee - _osrcc.ThisCostFee - _osrcc.ThisLateFee;
                    }

                    osrcc.UpdateThisFee(charge.ChargeMoneyDetailList[i].SrccId, charge.ChargeMoneyDetailList[i].ItemCode,
                                        _osrcc.Lastmaturity,
                                        _osrcc.Thismaturity,
                                        _osrcc.Lastsurplus,
                                        _osrcc.LastLateFee,
                                        _osrcc.ThisCostFee,
                                        _osrcc.ThisLateFee,
                                        _osrcc.FavourFee,
                                        _osrcc.RealityFee,
                                        _osrcc.PrepayFee);
                }
            }
            return true;
        }
        [AOP(typeof(AopTransaction))]
        public override bool CancelChargeMoney(string _chargeId, int _chargeEmpID)
        {
            //string srccId = null;
            //判断取消只能从最后一条有效的收银记录开始
            if (_chargeId != cashDao.GetLastChargeId())
                throw new Exception("选定取消的收银记录不是最后一次收银记录！");
            //1.WY_ChargeMoney 修改ChargeFlag=1
            AlterChargeFlag(_chargeId, 1);
            //2.插入一条负记录WY_ChargeMoney
            AbstractChargeMoney charge = NewObject<AbstractChargeMoney>();
            charge.ChargeID = _chargeId;
            AbstractChargeMoney _charge = (AbstractChargeMoney)charge.getmodel();
            //srccId = _charge.SrccId;

            _charge.Lastsurplus = -_charge.Lastsurplus;
            _charge.ReceivableFee = -_charge.ReceivableFee;
            _charge.FavourFee = -_charge.FavourFee;
            _charge.RealityFee = -_charge.RealityFee;
            _charge.Thissurplus = -_charge.Thissurplus;
            _charge.ChargeFlag = 1;
            _charge.ChargeDate = DateTime.Now;
            _charge.ChargeEmpID = _chargeEmpID;
            _charge.ChargeEmpName = CoreFrame.Business.CommonData.GetData("EmpName", _chargeEmpID).ToString();
            _charge.OldChargeID = _chargeId;
            _charge.ChargeID = null;
            _charge.save();
            //3.插入明细记录WY_ChargeMoneyDetail
            AbstractChargeMoneyDetail detail = NewObject<AbstractChargeMoneyDetail>();
            List<AbstractChargeMoneyDetail> detailList = detail.GetChargeDetailList(_chargeId);
            for (int i = 0; i < detailList.Count; i++)
            {
                AbstractChargeMoneyDetail _detail = detailList[i];
                _detail.OldChargeDetailID = _detail.ChargeDetailID;
                _detail.ChargeDetailID = null;
                _detail.ChargeID = _charge.ChargeID;
                _detail.Lastsurplus = -_detail.Lastsurplus;
                _detail.LastLateFee = -_detail.LastLateFee;
                _detail.ThisCostFee = -_detail.ThisCostFee;
                _detail.ThisLateFee = -_detail.ThisLateFee;
                _detail.FavourFee = -_detail.FavourFee;
                _detail.RealityFee = -_detail.RealityFee;
                _detail.PrepayFee = -_detail.PrepayFee;
                _detail.InitDao();
                _detail.save();
            }
            //4.清空ItemFeeHead chargeId字段
            AbstractItemFeeHead head = NewObject<AbstractItemFeeHead>();
            head.CancelChargeId(_chargeId);
            //5.AbstractOSRCCItemRelation 修改其金额字段
            AbstractOSRCCItemRelation osrcc = NewObject<AbstractOSRCCItemRelation>();
            detailList = detail.GetChargeDetailList(_chargeId);
            for (int i = 0; i < detailList.Count; i++)
            {
                AbstractOSRCCItemRelation _osrcc = osrcc.GetSrccItem(detailList[i].SrccId, detailList[i].ItemCode);
                if (_osrcc != null)
                {
                    _osrcc.Lastmaturity = detailList[i].Lastmaturity;
                    //_osrcc.Thismaturity = DateTime.Now;
                    _osrcc.Lastsurplus = detailList[i].Lastsurplus;
                    _osrcc.LastLateFee = detailList[i].LastLateFee;
                    _osrcc.ThisCostFee += detailList[i].ThisCostFee;
                    _osrcc.ThisLateFee += detailList[i].ThisLateFee;
                    _osrcc.FavourFee = 0;

                    //实收金额(暂)=应收金额+滞纳金-上次余额-上次滞纳金-优惠金额
                    //（如果 实收金额(暂)>=0）  实收金额=实收金额(暂) 、 本次余额=0
                    //（如果 实收金额(暂)<0 ）  实收金额=0 、 本次余额=实收金额+优惠金额+上次余额+上次滞纳金-应收金额-滞纳金
                    decimal _RealityFee = _osrcc.ThisCostFee + _osrcc.ThisLateFee - _osrcc.Lastsurplus - _osrcc.LastLateFee - _osrcc.FavourFee;
                    if (_RealityFee >= 0)
                    {
                        _osrcc.RealityFee = _RealityFee;
                        _osrcc.PrepayFee = 0;
                    }
                    else
                    {
                        _osrcc.RealityFee = 0;
                        _osrcc.PrepayFee = _osrcc.RealityFee + _osrcc.FavourFee + _osrcc.Lastsurplus + _osrcc.LastLateFee - _osrcc.ThisCostFee - _osrcc.ThisLateFee;
                    }

                    osrcc.UpdateThisFee(detailList[i].SrccId, detailList[i].ItemCode,
                                        _osrcc.Lastmaturity,
                                        _osrcc.Thismaturity,
                                        _osrcc.Lastsurplus,
                                        _osrcc.LastLateFee,
                                        _osrcc.ThisCostFee,
                                        _osrcc.ThisLateFee,
                                        _osrcc.FavourFee,
                                        _osrcc.RealityFee,
                                        _osrcc.PrepayFee);
                }
            }
            return true;
        }

        public override bool AlterAccountId(string accountId)
        {
            return cashDao.ChargeMoneyAlterAccountId(accountId);
        }

        public override List<AbstractChargeMoney> GetChargeMoneyList(int _chargeEmpId, DateTime _chargeDate)
        {
            throw new NotImplementedException();
        }

        public override List<AbstractChargeMoney> GetChargeMoneyList(string _srccId, PageInfo page)
        {
            return cashDao.GetChargeMoneyList(_srccId, page);
        }

        public override bool AlterChargeFlag(string chargeId, int flag)
        {
            return cashDao.AlterChargeFlag(chargeId, flag);
        }
    }
}
